﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forest.Lion.Data;
using Forest.Lion.Domain.Contracts;
using Forest.Lion.Domain.Models;
using Forest.Lion.Model;
using Forest.Lion.Infrastructure;
using Forest.Lion.Domain.Shared;

namespace Forest.Lion.Domain.Services
{
    public class JobPosiotionAppService
    {
        private readonly IJobPositionRepository _jobPositionRepository;
        private readonly IDepartmentRepository _departmentRepository;

        public JobPosiotionAppService(IJobPositionRepository jobPositionRepository, IDepartmentRepository departmentRepository)
        {
            _jobPositionRepository = jobPositionRepository;
            _departmentRepository = departmentRepository;
        }

        #region Create
        public void Create(IJobPositionFormModel jobPositionForm, Guid companyId)
        {
            if (jobPositionForm == null)
                throw new ArgumentNullException("jobPositionForm");

            try
            {
                var entity = jobPositionForm.ConvertToEntity();
                entity.Id = Guid.NewGuid();
                entity.CompanyId = companyId;
                entity.DateCreated = DateTime.Now;
                entity.LastUpdated = DateTime.Now;
                entity.CreatedBy = companyId;
                entity.IsExchange = false;
                entity.Source = 0;
                _jobPositionRepository.Create(entity);
            }
            catch (InvalidOperationException ex)
            {
                throw new BusinessServicesException("UnableToCreateVehicleExceptionMessage", ex);
            }
        }
        #endregion

        //#region DeleteById
        //public void DeleteById(Guid jobPositionId)
        //{
        //    var JobPositionToDelete = _jobPositionRepository.GetJobPosition(jobPositionId);
        //    if (JobPositionToDelete != null)
        //    {
        //        _jobPositionRepository.Delete(jobPositionId);
        //    }
        //}
        //#endregion

        #region GetById
        public JobPosition GetById(Guid jobPositionId)
        {
            return _jobPositionRepository.GetJobPosition(jobPositionId);
        }
        #endregion

        #region GetListByCompanyId
        public IPagedList<JobPosition> SearchList(Guid companyId,
            JobQueryStatus status,
            String keyword,
            int pageIndex,
            int pageSize)
        {
            IPagedList<JobPosition> list = null;
            switch (status)
            {
                case JobQueryStatus.All:
                    list = _jobPositionRepository.SearchList(companyId, null, false, keyword, pageIndex, pageSize);
                    break;
                case JobQueryStatus.New:
                    list = _jobPositionRepository.SearchList(companyId, JobPositionStatus.New, false, keyword, pageIndex, pageSize);
                    break;
                case JobQueryStatus.Active:
                    list = _jobPositionRepository.SearchList(companyId, JobPositionStatus.Active, false, keyword, pageIndex, pageSize);
                    break;
                case JobQueryStatus.Pause:
                    list = _jobPositionRepository.SearchList(companyId, JobPositionStatus.Pause, false, keyword, pageIndex, pageSize);
                    break;
                case JobQueryStatus.Closed:
                    //先把逾期的职位修改为Closed状态
                    var overdueList = _jobPositionRepository.GetOverdueList(companyId);
                    (overdueList as List<JobPosition>).ForEach(x =>
                    {
                        x.Status = (int)JobPositionStatus.Closed;
                        _jobPositionRepository.Update(x);
                    });
                    //然后再进行检索
                    list = _jobPositionRepository.SearchList(companyId, JobPositionStatus.Closed, false, keyword, pageIndex, pageSize);
                    break;
                case JobQueryStatus.Deleted:
                    list = _jobPositionRepository.SearchList(companyId, null, true, keyword, pageIndex, pageSize);
                    break;
            }
            return list;
        }
        #endregion

        #region Update
        public void Update(IJobPositionFormModel jobPositionForm)
        {
            var entity = _jobPositionRepository.GetJobPosition(jobPositionForm.Id);
            jobPositionForm.ConvertToEntity(entity);
            entity.LastUpdated = DateTime.Now;
            _jobPositionRepository.Update(entity);
        }
        #endregion

        #region 上线
        public void Post(Guid jobPositionId)
        {
            var entity = _jobPositionRepository.GetJobPosition(jobPositionId);
            if (entity.Status != (int)JobPositionStatus.Pause)
            {
                entity.StartDate = DateTime.Now.Date;
                entity.EndDate = DateTime.Now.Date.AddDays(30);
                entity.Reposted = DateTime.Now;
            }
            entity.Status = (int)JobPositionStatus.Active;
            entity.LastUpdated = DateTime.Now;
            _jobPositionRepository.Update(entity);
        }
        #endregion

        #region 暂停
        public void Pause(Guid jobPositionId)
        {
            var entity = _jobPositionRepository.GetJobPosition(jobPositionId);
            entity.Status = (int)JobPositionStatus.Pause;
            entity.LastUpdated = DateTime.Now;
            _jobPositionRepository.Update(entity);
        }
        #endregion

        #region 刷新
        public void Refresh(Guid jobPositionId)
        {
            var entity = _jobPositionRepository.GetJobPosition(jobPositionId);
            entity.Reposted = DateTime.Now;
            entity.LastUpdated = DateTime.Now;
            _jobPositionRepository.Update(entity);
        }
        #endregion

        #region 删除
        public void Delete(Guid jobPositionId)
        {
            var entity = _jobPositionRepository.GetJobPosition(jobPositionId);
            entity.Deleted = true;
            entity.LastUpdated = DateTime.Now;
            _jobPositionRepository.Update(entity);
        }
        #endregion

        #region 还原
        public void Restore(Guid jobPositionId)
        {
            var entity = _jobPositionRepository.GetJobPosition(jobPositionId);
            entity.Deleted = false;
            entity.LastUpdated = DateTime.Now;
            _jobPositionRepository.Update(entity);
        }
        #endregion

        #region 修改排序号
        public void ChangeSortOrder(Guid id, int sortOrder)
        {
            var entity = _jobPositionRepository.GetJobPosition(id);
            entity.SortOrder = sortOrder;
            _jobPositionRepository.Update(entity);
        } 
        #endregion
    }
}
